using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Universe.WinForms
{
    public class ToolStripIndeterminateProgressBar : ToolStripControlHost
    {
        public ToolStripIndeterminateProgressBar()
            : base(new IndeterminateProgressBar())
        {
            // ProgressBar.BackColor = Color.White;
            this.AutoSize = false;
        }

        [Category("Progress Bar")]
        public IndeterminateProgressBar ProgressBar
        {
            get { return Control as IndeterminateProgressBar; }
        }

        [Category("Progress Bar")]
        public bool Running
        {
            get
            {
                return ProgressBar.IsPulsing;
            }
            set
            {
                if (value && !ProgressBar.IsPulsing)
                    ProgressBar.Start();

                else if (!value && ProgressBar.IsPulsing)
                    ProgressBar.Stop();
            }
        }


        protected override void OnOwnerChanged(EventArgs e)
        {
            base.OnOwnerChanged(e);
/*
            ProfessionalColorTable ct = new ProfessionalColorTable();
            ct.UseSystemColors = false;
            Color bc = ct.StatusStripGradientEnd;

            ProgressBar.BackColor = bc;
*/
            ProgressBar.BackColor = Color.Transparent;
            
            // Debug.WriteLine("PB Back Color: " + bc + " = " + ProgressBar.BackColor);

            DoResize();

            
        }

        void DoResize()
        {
            // Debug.WriteLine("Set PB size: " + this.Size);
            ProgressBar.PipHeight = 11;
            ProgressBar.Pips = this.Size.Width / (ProgressBar.PipHeight + 4);
            int h = ProgressBar.PipHeight + 2;
            Size ps = new Size(this.Size.Width, h);
            ProgressBar.MaximumSize = ps;
            ProgressBar.MinimumSize = ps;
            ProgressBar.Size = ps;
        }

        protected override void OnHostedControlResize(EventArgs e)
        {
            base.OnHostedControlResize(e);
            DoResize();
        }

        protected override Size DefaultSize
        {
            get { return new Size(100,5); }
        }


        public override Size GetPreferredSize(Size constrainingSize)
        {
            return DefaultSize;
        }


        protected override void OnBackColorChanged(EventArgs e)
        {
            ProgressBar.BackColor = BackColor;
        }


    }
}
